<?php
/*
 * ---------------------------------------------------------------
* Author  : Anthony Tran
* Email   : Incredibletran@gmail.com - Incredibletran@hotmail.com
* Version : 1.2
* ---------------------------------------------------------------
*/

class Gallery_model extends CI_Model{

	var $table			=	"gallery";
	var $table_cate		=	"gallery_cate";
	public function __construct(){
		parent::__construct();
	}
	
	
	function get_all($cate = "all"){
		$this->db->select();
		if($cate != "all"){
			$this->db->where('cate_id', $cate);
		}							
		$query	=	$this->db->get($this->table);
		$res	=	$query->result();
		if(!empty($res)){
			return $res;
		}
		return false;
	}
	
	
	function add_image($info){
		return $this->db->insert($this->table, $info);
	}
	
	function get_album_info($album_id){
		$res	=	$this->db->select()
							 ->where('id', $album_id)
							 ->get($this->table_cate)
							 ->row_array();
		if(!empty($res)){
			return $res;
		}
		return false;
	}
	
	function get_list(){
		$this->db->join("gallery_cate as cate", 'cate.id = gallery.cate_id', 'left');
		$this->db->select("gallery.*, cate.name as parent_name");

		$this->db->order_by('cate.order', 'DESC');
		$query	=	$this->db->get($this->table);
		$result	=	$query->result();
		if(!empty($result)){
			return $result;
		}
		return false;
	}
	
	function get_photos_in_album($album_id){
		$photos	=	$this->db->select()
							 ->where('cate_id', $album_id)
							 ->order_by('id','ASC')
							 ->get($this->table)
							 ->result();
		if(!empty($photos)){
			return $photos;
		}
		return false;
	}
	
	
	function add($info	=	array()){
		$res	=	$this->db->insert($this->table, $info);
		return $res;
	}
	
	function edit($id, $info){
    	return $this->db->where('id', $id)
    					->update($this->table_cate, $info);
    }
	
	
	function delete($id ){
		return $this->db->where('id', $id)->delete($this->table);
	}
	
	function delete_album($albumid){
		return $this->db->where('id', $albumid)->delete($this->table_cate);
	}
	
	function get_cate(){
		return $this->db->select()
						->get($this->table_cate)
						->result();
	}
	
	
	function get_brothers($photo_id, $parent_id){
		$brothers = $this->db->select()
							 ->where('id !=', $photo_id)
							 ->where('cate_id', $parent_id)
							 ->get($this->table)
							 ->result();
		if(!empty($brothers)){
			return true;
		}
		return false;
	}

	
	function count_all(){
		return $this->db->select()->count_all($this->table);
	}
	
	
	function check_exist($name = ""){
		$res	=	$this->db->select()
							->where('title', $name)
							->get($this->table)
							->row_array();
		if(!empty($res)){
			return true;
		}
		return false;
	}
	

	
	function check_exist_edit($cate_id, $name){
		$res	=	$this->db->select()
							->where('title', $name)
							->where('cate_id !=', $cate_id)
							->get($this->table)
							->result();
		if(!empty($res)){
			return true;
		}
		return false;
	}
	
	
	function get_match($id = 0){
		$res	=	$this->db->select()
							->where('id', $id)
							->get($this->table)
							->row();
		if(!empty($res)){
			return $res;
		}
		return false;
	}
	
	
	
}

/*---------------------------------------------End-----------------------------------------*/